<template>
  <el-dialog
    :title="dialogTitle"
    style="margin-top: 0 !important;top: 60px"
    :visible.sync="visible"
    width="1300px"
    append-to-body
  >
    <el-form style="margin-top: -20px" :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="工号/学号" label-width="80px" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入工号/学号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="tableList" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工号/学号" align="center" prop="userId" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="150px" />
      <el-table-column label="姓名" align="center" prop="nickName" />
      <el-table-column label="电话" align="center" prop="phonenumber" width="120px" />
      <el-table-column label="身份证" align="center" prop="card" width="170px" />
      <el-table-column label="角色" align="center" prop="sysRoleList" :formatter="formatRole" />
      <el-table-column label="用户组" align="center" prop="groupList" :formatter="formatGroup" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span>{{ scope.row.status === '0' ? '启用' : '冻结' }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <div slot="footer" class="dialog-footer">
      <el-button :disabled="multiple" type="primary" @click="handleSubmit">提 交</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { listAccount } from '@/pages/views/account/manage/service'
  import { addUserToGroup } from '@/pages/views/userGroup/groupManage/service'
  export default {
    props: ['groupId'],
    data() {
      return {
        dialogTitle: '添加已有用户',
        // 遮罩层
        visible: false,
        // 表格遮罩层
        loading: false,
        // 选中数组
        ids: [],
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 表格数据
        tableList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 8
        }
      }
    },
    methods: {
      formatRole(row, column, cellValue, index) {
        return Array.isArray(cellValue) ? cellValue.map(item => item.roleName).join('，') : '-'
      },
      formatGroup(row, column, cellValue, index) {
        return Array.isArray(cellValue) ? cellValue.map(item => item.userGroupName).join('，') : '-'
      },
      // 显示弹框
      show() {
        this.visible = true
        this.getList()
      },
      /** 查询列表 */
      getList() {
        this.loading = true
        listAccount(this.queryParams)
          .then(res => {
            if (res.code === 200) {
              console.log(111111111, res)
              this.tableList = res.rows
              this.total = res.total
            }
          })
          .finally(() => {
            this.loading = false
          })
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.userId)
        this.multiple = !selection.length
      },
      /* 提交 */
      handleSubmit() {
        addUserToGroup({ groupId: this.groupId[0], userIds: this.ids }).then(res => {
          this.msgSuccess(res.msg)
          if (res.code === 200) {
            this.visible = false
            this.$emit('ok')
          }
        })
      }
    }
  }
</script>
